﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using ErpCoreModel.Framework;
using ErpCoreModel.Base;
using MongoDB.Driver;
using MongoDB.Bson;

namespace ErpCore
{
    public partial class Form1 : Form
    {
        CUserMgr m_baseMgr = null;

        public delegate void myEventHandler(string userStatus);
        public void ChangeLabelText(string sText)
        {
            lbRec.Text = sText;
        } 

        public Form1()
        {
            InitializeComponent();

            string sPath = Application.StartupPath + "\\config.cfg";
            if (!File.Exists(sPath))
            {
                DbSetup frmDb = new DbSetup();
                if (frmDb.ShowDialog() != DialogResult.OK)
                {
                    Application.Exit();
                    return;
                }
            }
            string sConnectionString = ErpCore.Util.DESEncrypt.DesDecrypt(File.ReadAllText(sPath));
            Program.Ctx.ConnectionString = sConnectionString;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!m_bIsStart)
            {
                m_bIsStart = true;
                timer1.Enabled = true;
                Thread thread = new Thread(new ThreadStart(trd));
                thread.Start();
            }
            else
            {
                m_bIsStart = false;
                timer1.Enabled = false;
            }
        }

        bool m_bIsStart = false;
        void trd()
        {


            double dblCount = 0;
            while (m_bIsStart)
            {
                //for (int i = 0; i < 100; i++)
                //{
                CUser user = new CUser();
                user.Ctx = Program.Ctx;
                user.Name = string.Format("user{0}", dblCount);
                user.TName = user.Name;

                CUserMgr baseMgr = new CUserMgr();
                baseMgr.Ctx = Program.Ctx;
                baseMgr.AddNew(user, true);

                    dblCount++;
                    if (dblCount >= 10000000)
                        break;

                    this.Invoke(new myEventHandler(ChangeLabelText), new object[] { dblCount.ToString() });
                //}
                //baseMgr.Save(true);
            }
        }

        long m_ltime = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            m_ltime++;
            lbTime.Text = m_ltime.ToString();
        }


        private void button5_Click(object sender, EventArgs e)
        {
            DateTime now1 = DateTime.Now;
            CUser user0 = m_baseMgr.FindByName(textBox1.Text.Trim());
            DateTime now2 = DateTime.Now;
            TimeSpan span = now2 - now1;

            string sText = string.Format("FindByName：{0} ", span.TotalSeconds);
            MessageBox.Show(sText);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CTable table = (CTable)Program.Ctx.TableMgr.GetFirstObj();
            table.ColumnMgr.GetList();
        }


    }
}
